using Microsoft.Expression.Interactivity.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Microsoft.Expression.Interactivity
{
	public static class VisualStateUtilities
	{
		public static bool GoToState(FrameworkElement element, string stateName, bool useTransitions)
		{
			bool result = false;
			if (!string.IsNullOrEmpty(stateName))
			{
				Control control = element as Control;
				if (control != null)
				{
					control.ApplyTemplate();
					result = VisualStateManager.GoToState(control, stateName, useTransitions);
				}
				else
				{
					result = ExtendedVisualStateManager.GoToElementState(element, stateName, useTransitions);
				}
			}
			return result;
		}
		public static IList GetVisualStateGroups(FrameworkElement targetObject)
		{
			IList list = new List<VisualStateGroup>();
			if (targetObject != null)
			{
				list = VisualStateManager.GetVisualStateGroups(targetObject);
				if (list.get_Count() == 0)
				{
					int childrenCount = VisualTreeHelper.GetChildrenCount(targetObject);
					if (childrenCount > 0)
					{
						FrameworkElement frameworkElement = VisualTreeHelper.GetChild(targetObject, 0) as FrameworkElement;
						list = VisualStateManager.GetVisualStateGroups(frameworkElement);
					}
				}
			}
			return list;
		}
		public static bool TryFindNearestStatefulControl(FrameworkElement contextElement, out FrameworkElement resolvedControl)
		{
			FrameworkElement frameworkElement = contextElement;
			if (frameworkElement == null)
			{
				resolvedControl = null;
				return false;
			}
			FrameworkElement frameworkElement2 = frameworkElement.get_Parent() as FrameworkElement;
			bool result = true;
			while (!VisualStateUtilities.HasVisualStateGroupsDefined(frameworkElement) && VisualStateUtilities.ShouldContinueTreeWalk(frameworkElement2))
			{
				frameworkElement = frameworkElement2;
				frameworkElement2 = (frameworkElement2.get_Parent() as FrameworkElement);
			}
			if (VisualStateUtilities.HasVisualStateGroupsDefined(frameworkElement))
			{
				FrameworkElement frameworkElement3 = VisualTreeHelper.GetParent(frameworkElement) as FrameworkElement;
				if (frameworkElement3 != null && frameworkElement3 is Control)
				{
					frameworkElement = frameworkElement3;
				}
			}
			else
			{
				result = false;
			}
			resolvedControl = frameworkElement;
			return result;
		}
		private static bool HasVisualStateGroupsDefined(FrameworkElement frameworkElement)
		{
			return frameworkElement != null && VisualStateManager.GetVisualStateGroups(frameworkElement).get_Count() != 0;
		}
		internal static FrameworkElement FindNearestStatefulControl(FrameworkElement contextElement)
		{
			FrameworkElement result = null;
			VisualStateUtilities.TryFindNearestStatefulControl(contextElement, out result);
			return result;
		}
		private static bool ShouldContinueTreeWalk(FrameworkElement element)
		{
			if (element == null)
			{
				return false;
			}
			if (element is UserControl)
			{
				return false;
			}
			if (element.get_Parent() == null)
			{
				FrameworkElement frameworkElement = VisualStateUtilities.FindTemplatedParent(element);
				if (frameworkElement == null || (!(frameworkElement is Control) && !(frameworkElement is ContentPresenter)))
				{
					return false;
				}
			}
			return true;
		}
		private static FrameworkElement FindTemplatedParent(FrameworkElement parent)
		{
			return VisualTreeHelper.GetParent(parent) as FrameworkElement;
		}
	}
}
